<?php
class XcxController extends ControllerBase
{
    public $params;
	public function initialize()
	{
//	    	parent::initialize();
        $this->params = array_merge($this->request->get(), $this->request->getPost());
	}


    /**
     * 小程序获取用户的个人信息
     */
    public function getxcxuserinfoAction(){
        if(!isset($this->params['code']) || empty($this->params['code'])){
            $this->fail(-1,'请传入code码');
        }
        $xcxuserinfo = $this->xcx->getXcxUserInfo($this->params['code']);
        if(isset($xcxuserinfo['errcode'])){
            $this->fail(-1,$xcxuserinfo['errmsg']);
        }
        $this->ok($xcxuserinfo);
    }


    /**
     * 小程序解密数据
     */
    public function getserinfodecodeAction(){
        $this->params = array_merge($this->request->get(), $this->request->getPost());
        if(!isset($this->params['sessionKey']) || empty($this->params['sessionKey'])){
            $this->fail(-1,'sessionKey不能为空');
        }
        if(!isset($this->params['encryptedData']) || empty($this->params['encryptedData'])){
            $this->fail(-1,'encryptedData');
        }
        if(!isset($this->params['iv']) || empty($this->params['iv'])){
            $this->fail(-1,'iv不能为空');
        }
        $userinfodecode = $this->xcx->userinfodecode($this->params['sessionKey'],$this->params['encryptedData'],$this->params['iv']);
        if(empty($userinfodecode)){
            $this->fail(-1,'解析数据失败');
        }
        $this->ok($userinfodecode);
    }


    /**
     * 获取小程序码
     */
    public function getaccesstokenAction(){
        if(!isset($this->params['invite_code']) || empty($this->params['invite_code'])){
            $this->fail(-1,'邀请码不能为空');
        }
        $redis_key="getaccesstoken";
        $cache = new BaseModel();
        $accesstoken = $cache->get_cache($redis_key);
        if(!$accesstoken){
            $result_accesstoken = $this->xcx->getaccesstoken();
            if(!$result_accesstoken['code']){
                $this->fail(-1,$result_accesstoken['msg']);
            }
            $accesstoken=$result_accesstoken['data'];
            $cache->set_cache($redis_key,$accesstoken,7000);
        }
        //$h_params['access_token']=$accesstoken;
        $h_params['scene']=$this->params['invite_code'];
        $h_params['page']='pages/home/home';
        $result = $this->xcx->getcode($h_params,$accesstoken);
        if($result['code']){
            $base64_image ="data:image/jpeg;base64,".base64_encode( $result['msg'] );
            $this->ok($base64_image);
        }else{
            $this->fail(-1,$result['msg']);
        }
    }


    /**
     * 微信小程序详情码
     */
    public function getxcxdetailAction(){
        if(!isset($this->params['site']) || empty($this->params['site'])){
            $this->fail(-1,'站点不能为空');
        }

        if(!isset($this->params['goods_id']) || empty($this->params['goods_id'])){
            $this->fail(-1,'商品详情不能为空');
        }
        if(!isset($this->params['invite_code']) || empty($this->params['invite_code'])){
            $this->fail(-1,'邀请码不能为空');
        }
        $redis_key="getaccesstoken";
        $cache = new BaseModel();
        $accesstoken = $cache->get_cache($redis_key);
        if(!$accesstoken){
            $result_accesstoken = $this->xcx->getaccesstoken();
            if(!$result_accesstoken['code']){
                $this->fail(-1,$result_accesstoken['msg']);
            }
            $accesstoken=$result_accesstoken['data'];
            $cache->set_cache($redis_key,$accesstoken,7000);
        }
        //$h_params['access_token']=$accesstoken;
        $h_params['scene']=$this->params['goods_id'].'_'.$this->params['invite_code'].'_'.$this->params['site'];
        $h_params['page']='pages/goods-details/goods-details';
        $result = $this->xcx->getcode($h_params,$accesstoken);
        if($result['code']){
            $base64_image ="data:image/jpeg;base64,".base64_encode( $result['msg'] );
            $this->ok($base64_image);
        }else{
            $this->fail(-1,$result['msg']);
        }
    }



}
